//
//  ForgetPswViewController.m
//  QXShowing
//
//  Created by 王道道 on 2018/4/22.
//  Copyright © 2018年 doll_dream. All rights reserved.
//

#define kTopMarginHeight   35
#define kLeftMarginHeight   20
#define kBgViewHeight   180+(kTopMarginHeight-20)*2

#import "ForgetPswViewController.h"

#import "UIButton+Countdown.h"

@interface ForgetPswViewController ()

@property (nonatomic, strong) UITextField *phoneTF;
@property (nonatomic, strong) UITextField *pswTF;
@property (nonatomic, strong) UITextField *codeTF;
@property (nonatomic, strong) UIButton *forgetBtn;
@property (nonatomic, strong) UIButton *codeBtn;
@property (nonatomic, strong) UIImageView *photoImage;

@end

@implementation ForgetPswViewController

- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
    [self.navigationController setNavigationBarHidden:YES animated:YES];
}

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    [self initLoginNavBar:@"忘记密码"];
    self.view.backgroundColor = Bg_Color_F9F9F9;
    [self setUpUI];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (void)setUpUI {
    self.photoImage = [[UIImageView alloc] init];
    self.photoImage.image = [UIImage imageNamed:@"login-6"];
    [self.view addSubview:self.photoImage];
    [self.photoImage mas_makeConstraints:^(MASConstraintMaker *make) {
        make.width.height.offset(100);
        make.top.equalTo(self.view.mas_top).offset(30+kNavbarAndStatusBar);//80
        make.centerX.equalTo(self.view);
    }];
    
    UILabel *placeLabel = [[UILabel alloc] init];
    placeLabel.text = @"请输入已注册或已绑定的手机号";
    placeLabel.textColor = Text_Color_999999;
    placeLabel.font = BHFont_Size_12;
    [self.view addSubview:placeLabel];
    [placeLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.photoImage.mas_bottom).offset(20);
        make.centerX.equalTo(self.view);
    }];
    
    UIImageView *bgView = [[UIImageView alloc] init];
    bgView.image = [UIImage imageNamed:@"login-3"];
//    bgView.contentMode = UIViewContentModeRedraw;

//    bgView.layer.borderWidth = 1;
//    bgView.layer.borderColor = Bottom_Line_Color.CGColor;
    bgView.userInteractionEnabled = YES;
    bgView.backgroundColor = [UIColor clearColor];
    [self.view addSubview:bgView];
    [bgView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.view.mas_left).offset(10);
//        make.top.equalTo(self.photoImage.mas_bottom).offset(30);//45
        make.top.equalTo(placeLabel.mas_bottom).offset(10);//45
        make.centerX.equalTo(self.view);
        make.height.offset(kBgViewHeight);//180
    }];
    
    UILabel *phoneLabel = [[UILabel alloc] init];
    phoneLabel.text = @"手机号码";
    phoneLabel.textColor = Text_Color_090909;
    phoneLabel.font = BHFont_Size_15;
    [bgView addSubview:phoneLabel];
    [phoneLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(bgView.mas_left).offset(kLeftMarginHeight);//15
        make.top.equalTo(bgView.mas_top).offset(kTopMarginHeight);//20
//        make.width.offset(80);
        make.width.offset([BHSettingUtil widthForText:@"设置新密码" fontSize:15]-10);
    }];
    
    UIView *line = [[UIView alloc] init];
    line.backgroundColor = Bottom_Line_Color;
    [bgView addSubview:line];
    [line mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(bgView.mas_left).offset(kLeftMarginHeight);
        make.height.offset(0.5);
        make.centerX.equalTo(bgView);
        make.top.equalTo(phoneLabel.mas_bottom).offset(20);
    }];
    
    UILabel *codeLabel = [[UILabel alloc] init];
    codeLabel.text = @"验证码";
    codeLabel.textColor = Text_Color_090909;
    codeLabel.font = BHFont_Size_15;
    [bgView addSubview:codeLabel];
    [codeLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(phoneLabel.mas_left);
        make.centerY.equalTo(bgView);
//        make.width.offset(80);
        make.width.equalTo(phoneLabel.mas_width);
    }];
    
    UIView *line1 = [[UIView alloc] init];
    line1.backgroundColor = Bottom_Line_Color;
    [bgView addSubview:line1];
    [line1 mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(line.mas_left);
        make.height.offset(0.5);
        make.centerX.equalTo(bgView);
        make.bottom.equalTo(codeLabel.mas_bottom).offset(20);
    }];
    
    UILabel *pswLabel = [[UILabel alloc] init];
    pswLabel.text = @"设置新密码";
    pswLabel.textColor = Text_Color_090909;
    pswLabel.font = BHFont_Size_15;
    [bgView addSubview:pswLabel];
    [pswLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(phoneLabel.mas_left);
        make.bottom.equalTo(bgView.mas_bottom).offset(-kTopMarginHeight);//-20
//        make.width.offset(80);
        make.width.equalTo(phoneLabel.mas_width);
    }];
    
    self.phoneTF = [[UITextField alloc] init];
    self.phoneTF.placeholder = @"请输入手机号";
    self.phoneTF.font = BHFont_Size_15;
    self.phoneTF.keyboardType = UIKeyboardTypePhonePad;
    [bgView addSubview:self.phoneTF];
    [self.phoneTF mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(phoneLabel.mas_right).offset(0);//15
        make.right.equalTo(bgView.mas_right).offset(-10);
        make.centerY.equalTo(phoneLabel);
        make.height.offset(50);
    }];
    
    self.codeTF = [[UITextField alloc] init];
    self.codeTF.placeholder = @"请输入验证码";
    self.codeTF.font = BHFont_Size_15;
    [bgView addSubview:self.codeTF];
    [self.codeTF mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.phoneTF.mas_left);
        make.right.equalTo(self.phoneTF.mas_right).offset(-85-kLeftMarginHeight);
        make.centerY.equalTo(codeLabel);
        make.height.offset(50);
    }];
    
    self.pswTF = [[UITextField alloc] init];
    self.pswTF.placeholder = @"请输入6-12位字母和数字组合";
    self.pswTF.font = BHFont_Size_15;
    self.pswTF.secureTextEntry = YES;
    [bgView addSubview:self.pswTF];
    [self.pswTF mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.phoneTF.mas_left);
        make.right.equalTo(self.phoneTF.mas_right);
        make.centerY.equalTo(pswLabel);
        make.height.offset(50);
    }];
    
    self.codeBtn = [[UIButton alloc] init];
    [self.codeBtn setBackgroundColor:Bg_Color_333333];
    [self.codeBtn setTitle:@"获取验证码" forState:UIControlStateNormal];
    [self.codeBtn setTitleColor:Text_Color_FFFFFF forState:UIControlStateNormal];
    [self.codeBtn addTarget:self action:@selector(codeButtonClick:) forControlEvents:UIControlEventTouchUpInside];
    self.codeBtn.titleLabel.font = BHFont_Size_12;
    [bgView addSubview:self.codeBtn];
    [self.codeBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.equalTo(bgView.mas_right).offset(-kLeftMarginHeight);
        make.centerY.equalTo(codeLabel);
        make.width.offset(85);
        make.height.offset(30);
    }];
    
    self.forgetBtn = [[UIButton alloc] init];
    [self.forgetBtn setBackgroundColor:Bg_Color_333333];
    [self.forgetBtn setTitle:@"提交" forState:UIControlStateNormal];
    [self.forgetBtn setTitleColor:Text_Color_FFFFFF forState:UIControlStateNormal];
    [self.forgetBtn addTarget:self action:@selector(forgetPswAction) forControlEvents:UIControlEventTouchUpInside];
    self.forgetBtn.titleLabel.font = BHFont_Size_17;
    self.forgetBtn.layer.masksToBounds = YES;
    self.forgetBtn.layer.cornerRadius = 10;
    [self.view addSubview:self.forgetBtn];
    [self.forgetBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.view.mas_left).offset(10);
        make.top.equalTo(bgView.mas_bottom).offset(50);//80
        make.centerX.equalTo(self.view);
        make.height.offset(55);
    }];
}

#pragma 按钮事件
//获取验证码
- (void)codeButtonClick:(UIButton *)sender {
    if ([BHSettingUtil dataAndStringIsNull:self.phoneTF.text]) {
        [[BHAlertUtil alertManager]showPromptInfo:@"请输入手机号"];
    }else {
        [sender startWithTime:120 title:@"获取验证码" countDownTitle:@"已发送" mainColor:[UIColor colorWithHexString:@"#6F6F6F"] countColor:[UIColor colorWithHexString:@"#B9B9B9"]];
        self.phoneTF.userInteractionEnabled = NO;
        dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(120 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
            self.phoneTF.userInteractionEnabled = YES;
        });
        
        [self requestPostSendCode];
    }
}

- (void)forgetPswAction {
    if ([BHSettingUtil dataAndStringIsNull:self.phoneTF.text]) {
        [[BHAlertUtil alertManager]showPromptInfo:@"请输入手机号"];
    }else if ([BHSettingUtil dataAndStringIsNull:self.codeTF.text]) {
        [[BHAlertUtil alertManager]showPromptInfo:@"请输入验证码"];
    }else if ([BHSettingUtil dataAndStringIsNull:self.pswTF.text]) {
        [[BHAlertUtil alertManager]showPromptInfo:@"请输入密码"];
    }else if (![BHSettingUtil validatePassword:self.pswTF.text]) {
        [[BHAlertUtil alertManager]showPromptInfo:@"请输入密码(6-12字母和数字组成)"];
    }else {
        [self requestPostUpdatePassword];
    }
}

#pragma 接口
- (void)requestPostSendCode {
    [[BHAlertUtil alertManager]showLoading:nil];
    NSMutableDictionary *params = [NSMutableDictionary dictionary];
    [params setObject:self.phoneTF.text forKey:@"telno"];
    [PPHTTPRequest requestPostSendCode:^(id response, BOOL success) {
        [[BHAlertUtil alertManager]hiddenLoading];
        if (success) {
            [[BHAlertUtil alertManager]showPromptInfo:@"发送成功"];
        }else {
            self.phoneTF.userInteractionEnabled = YES;
            [self.codeBtn stopWithButtonmainColor:[UIColor colorWithHexString:@"#6F6F6F"] title:@"获取验证码"];
        }
    } params:params];
}

//找回密码
- (void)requestPostUpdatePassword {
    NSMutableDictionary *params = [NSMutableDictionary dictionary];
    [params setObject:self.phoneTF.text forKey:@"telno"];
    [params setObject:self.codeTF.text forKey:@"smscode"];
    [params setObject:self.pswTF.text forKey:@"newpassword"];
    [PPHTTPRequest requestPostFindPassword:^(id response, BOOL success) {
        if (success) {
            [[BHAlertUtil alertManager]showPromptInfo:@"找回密码成功，请使用新密码登录"];
            [self.navigationController popViewControllerAnimated:YES];
        }
    } params:params];
}


@end
